package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverOilRechargeBiz;
import com.egao.orderB.biz.DriverOilSonCardBiz;
import com.egao.orderB.entity.DriverOilMainCard;
import com.egao.orderB.entity.DriverOilRecharge;
import com.egao.orderB.entity.DriverOilSonCard;
import com.egao.orderB.service.DriverOilMainCardService;
import com.egao.orderB.service.DriverOilSonCardService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

//import com.egao.orderB.mapper.DriverOilSonCardMapper;
//import javax.transaction.Transactional;

/**
 * 油卡服务实现类
 * @author : Doman
 * @date : 2021-07-07 16:54:58
 * @version ：1.0
 */
@Service
public class DriverOilSonCardServiceImpl /*extends ServiceImpl<DriverOilSonCardMapper, DriverOilSonCard> */implements DriverOilSonCardService {

	@Autowired
	private DriverOilSonCardBiz driverOilSonCardBiz;
	@Autowired
	private DriverOilMainCardService driverOilMainCardService;
	@Autowired
	private DriverOilRechargeBiz driverOilRechargeBiz;

    /**
    @Override
    public PageResult<DriverOilSonCard> listPage(PageParam<DriverOilSonCard> page) {
        List<DriverOilSonCard> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<DriverOilSonCard> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, DriverOilSonCard driverOilSonCard, User loginUser)  throws Exception{
        return driverOilSonCardBiz.list(page,driverOilSonCard,loginUser);
    }

    @Override
    public JsonResult list(DriverOilSonCard driverOilSonCard, User loginUser) {
        return JsonResult.ok().setData(driverOilSonCardBiz.list(driverOilSonCard,loginUser));
    }

    @Override
    public JsonResult getById(DriverOilSonCard driverOilSonCard) {
        return JsonResult.ok().setData(driverOilSonCardBiz.getById(driverOilSonCard));
    }

    @Transactional
    @Override
    public JsonResult save(DriverOilSonCard driverOilSonCard, User loginUser) {
        if (driverOilSonCardBiz.save(driverOilSonCard, loginUser)) {
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(DriverOilSonCard driverOilSonCard, User loginUser) {
        if (driverOilSonCardBiz.update(driverOilSonCard, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Transactional
    @Override
    public JsonResult distribution(DriverOilSonCard driverOilSonCard, User loginUser) {
        if (driverOilSonCardBiz.distribution(driverOilSonCard, loginUser)) {
            DriverOilMainCard driverOilMainCard = new DriverOilMainCard();
            driverOilMainCard.setId(driverOilSonCard.getMainCardId()+"");
            if(Utils.isNotEmpty(driverOilSonCard.getPrice())){
                driverOilMainCard.setPrice(driverOilSonCard.getPrice());
            }
            if(Utils.isNotEmpty(driverOilSonCard.getOil())){
                driverOilMainCard.setOil(driverOilSonCard.getOil());
            }
            driverOilMainCardService.distribution(driverOilMainCard, loginUser);

            DriverOilSonCard driverOilSonCard2 = driverOilSonCardBiz.getById(driverOilSonCard);
            DriverOilRecharge driverOilRecharge = new DriverOilRecharge();
            driverOilRecharge.setDriverId(driverOilSonCard2.getDriverId());
            driverOilRecharge.setDriver(driverOilSonCard2.getDriver());
            driverOilRecharge.setCarNo(driverOilSonCard2.getCarNo());
            driverOilRecharge.setOilCard(driverOilSonCard2.getOilCard());
            driverOilRecharge.setOil(driverOilSonCard.getOil());
            driverOilRecharge.setReDate(driverOilSonCard.getReDate());
            driverOilRecharge.setPrice(driverOilSonCard.getPrice());
            driverOilRecharge.setType("分配");
            driverOilRecharge.setRemark("子卡分配");
            driverOilRechargeBiz.save(driverOilRecharge, loginUser);
            return JsonResult.ok("子卡分配成功");
        }
        return JsonResult.error("子卡分配失败");
    }

    @Transactional
    @Override
    public JsonResult deduction(DriverOilSonCard driverOilSonCard, User loginUser) {
        if (driverOilSonCardBiz.deduction(driverOilSonCard, loginUser)) {
            DriverOilSonCard driverOilSonCard2 = driverOilSonCardBiz.getById(driverOilSonCard);
            DriverOilRecharge driverOilRecharge = new DriverOilRecharge();
            driverOilRecharge.setDriverId(driverOilSonCard2.getDriverId());
            driverOilRecharge.setDriver(driverOilSonCard2.getDriver());
            driverOilRecharge.setCarNo(driverOilSonCard2.getCarNo());
            driverOilRecharge.setOilCard(driverOilSonCard2.getOilCard());
            driverOilRecharge.setOil(driverOilSonCard.getOil());
            driverOilRecharge.setReDate(driverOilSonCard.getReDate());
            driverOilRecharge.setPrice(driverOilSonCard.getPrice());
            driverOilRecharge.setType("扣款");
            driverOilRecharge.setRemark("子卡扣款");
            driverOilRechargeBiz.save(driverOilRecharge, loginUser);
            return JsonResult.ok("子卡扣款成功");
        }
        return JsonResult.error("子卡扣款失败");
    }

    @Transactional
    @Override
    public JsonResult remove(DriverOilSonCard driverOilSonCard, User loginUser) {
        if (driverOilSonCardBiz.remove(driverOilSonCard, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
